package com.blrs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.RepeatSubmit;
import com.blrs.common.constant.HttpStatus;
import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.domain.ElectricSalesInfo;
import com.blrs.domain.QuotationInfo;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.service.enterprise.businessinfo.IBusinessOpportunityInformationService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.BusinessOpportunityInformation;

import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-09-22
 */
@RestController
@RequestMapping("/business/information")
public class BusinessOpportunityInformationController extends BaseController
{
    @Autowired
    private IBusinessOpportunityInformationService businessOpportunityInformationService;

    /**
     * 查询【请填写功能名称】列表
     */
    //@PreAuthorize("@ss.hasPermi('business:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessOpportunityInformation businessOpportunityInformation,@RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
       //startPage();
        List<BusinessOpportunityInformation> list = businessOpportunityInformationService.selectBusinessOpportunityInformationList(businessOpportunityInformation);
        if (list.size() > 15){
            //分页失效情况,取前15条数据
            int start = (pageNum - 1) * pageSize;
            int end = Math.min(start + pageSize, list.size());
            List<BusinessOpportunityInformation>   businessOpportunityInformations = list.subList(start, end);
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("查询成功");
            rspData.setRows(businessOpportunityInformations);
            rspData.setTotal(new PageInfo(list).getTotal());
            return rspData;
        }else {
          return getDataTable(list);
         }

    }

    //旧（2025-3-11）报价单审核只需要展示报价单内容
//    @GetMapping("/reviewList")
//    public TableDataInfo reviewList(BusinessOpportunityInformation businessOpportunityInformation)
//    {
//        startPage();
//        List<BusinessOpportunityInformation> list = businessOpportunityInformationService.selectBusinessOpportunityInformationReviewList(businessOpportunityInformation);
//        return getDataTable(list);
//    }
    @GetMapping("/reviewList")
    public TableDataInfo reviewList(QuotationInfo quotationInfo) {
        startPage();
        List<QuotationInfo> quotationInfos = businessOpportunityInformationService.selectQuotationInfo(quotationInfo);
        return getDataTable(quotationInfos);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "商机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessOpportunityInformation businessOpportunityInformation)
    {
        List<BusinessOpportunityInformation> list = businessOpportunityInformationService.selectBusinessOpportunityInformationList(businessOpportunityInformation);
        ExcelUtil<BusinessOpportunityInformation> util = new ExcelUtil<BusinessOpportunityInformation>(BusinessOpportunityInformation.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:information:query')")
    @GetMapping(value = "/{businessId}")
    public AjaxResult getInfo(@PathVariable("businessId") String businessId)
    {
        return success(businessOpportunityInformationService.selectBusinessOpportunityInformationByBusinessId(businessId));
    }

    /**
     * 新增【请填写功能名称】
     */
    //@PreAuthorize("@ss.hasPermi('business:information:add')")
    @Log(title = "商机信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult add(@RequestBody BusinessOpportunityInformation businessOpportunityInformation) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(businessOpportunityInformationService.insertBusinessOpportunityInformation(businessOpportunityInformation));
    }

    /**
     * 修改【请填写功能名称】
     */
    //@PreAuthorize("@ss.hasPermi('business:information:edit')")
    @Log(title = "商机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(interval = 3000,message = "正在修改/审核，请勿重复提交！")
    public AjaxResult edit(@RequestBody BusinessOpportunityInformation businessOpportunityInformation) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(businessOpportunityInformationService.updateBusinessOpportunityInformation(businessOpportunityInformation));
    }

    /**
     * 删除【请填写功能名称】
     */
    //@PreAuthorize("@ss.hasPermi('business:information:remove')")
    @Log(title = "商机信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{businessIds}")
    public AjaxResult remove(@PathVariable String[] businessIds)
    {
        return toAjax(businessOpportunityInformationService.deleteBusinessOpportunityInformationByBusinessIds(businessIds));
    }

    @Log(title = "商机信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BusinessOpportunityInformation> util = new ExcelUtil(BusinessOpportunityInformation.class);
        List<BusinessOpportunityInformation> list = util.importExcel(file.getInputStream());
        String message = businessOpportunityInformationService.importExcel(list);
        return success(message);
    }
    @PostMapping("/editMainClass")
    public AjaxResult editMainClass(@RequestBody BusinessMainClassVo businessMainClassVo) {
        return toAjax(businessOpportunityInformationService.editMainClass(businessMainClassVo));
    }

    @PostMapping("/editBusinessManager")
    public AjaxResult editBusinessManager(@RequestBody BusinessMainClassVo businessMainClassVo) {
        return toAjax(businessOpportunityInformationService.editBusinessManager(businessMainClassVo));
    }

    /**
     * 导出商机管理模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BusinessOpportunityInformation> util = new ExcelUtil<>(BusinessOpportunityInformation.class);
        util.importTemplateExcel(response, "公海管理数据");
    }

    @GetMapping("/groupBusinessProvider")
    public AjaxResult groupBusinessProvider(){
        return success(businessOpportunityInformationService.groupBusinessProvider());
    }
}
